<html>
<head>
<style type="text/css">
	body	{ font-family: sans-serif; font-size: 10px; }
</style>
</head>
<body>
<?php
echo "Loading...";

require ("imdb.class.php");

include 'db.inc.php';
$imdb_ids = array();
$imdb_idonly = array();
$thelist = file("http://www.imdb.com/mymovies/list?l=15002934");

$thelist = preg_grep("/\/title\/tt\d+\//",$thelist);
foreach($thelist as $temp){
	$matches = array();
	if(preg_match("/^.+?title\/tt(\d+)\/.+?\>(.+?)\</",$temp,$matches)){
		$imdb_ids[$matches[1]] = array("ID"=>$matches[1],"title"=>$matches[2]);
		array_push($imdb_idonly,$matches[1]);
	}
}

$sql = mysql_query("SELECT * FROM m_movies");
$imdb_already = array();
if(mysql_num_rows($sql) > 0){
	while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
		array_push($imdb_already, $row["imdbID"]);
	}
}

$toget = array_diff($imdb_idonly,$imdb_already);

echo "New movies:<br />\n";
foreach($toget as $ID){
	echo $imdb_ids[$ID]["title"]."<br />\n";
}

// print_r($imdb_already);

foreach($toget as $ID){
	ob_flush();
	flush();
	
	$movie = new imdb ($ID);
	$movieid = $ID;

	$movie->setid ($movieid);
	$cols = $movie->colors ();
	$sound = $movie->sound ();
	$country = $movie->country ();

	if(!is_array($cols)){ $cols = array($cols); }
	if(!is_array($akas)){ $akas = array($akas); }
	if(!is_array($country)){ $country = array($country); }
	if(!is_array($sound)){ $sound = array($sound); }

	echo $movie->title()."<br />";

	$akas = array();
	foreach ( $movie->alsoknow() as $ak){
		array_push($akas,$ak["title"].": ".$ak["year"].", ".$ak["country"]." (".$ak["comment"].")");
	}

	mysql_query("INSERT INTO m_movies SET imdbID=\"".$ID."\", title=\"".mysql_real_escape_string($movie->title())."\", imdbrating=\"".$movie->rating()."\", imdbraters=".round(str_replace(",","",$movie->votes())).", runtime=".round($movie->runtime()).", color=\"".mysql_real_escape_string(implode(",",$cols))."\", year=\"".$movie->year()."\", language=\"".$movie->language()."\", aka=\"".mysql_real_escape_string(implode("\n",$akas))."\", country=\"".mysql_real_escape_string(implode(",",$country))."\", sound=\"".mysql_real_escape_string(implode(",",$sound))."\"");

	$movSQLid = mysql_insert_id();

	if(mysql_insert_id() > 0){

		$setgenres = array();
		mysql_query("INSERT INTO m_genre SET movID=".$movSQLid.", genreName=\"".mysql_real_escape_string($movie->genre())."\", mainGenre=\"1\"");
		array_push($setgenres, $movie->genre());
	
		$gen = $movie->genres ();
		foreach($gen as $genre){
			if(in_array($genre, $setgenres)){ continue; }
			mysql_query("INSERT INTO m_genre SET movID=".$movSQLid.", genreName=\"".mysql_real_escape_string($genre)."\", mainGenre=\"0\"");
			array_push($setgenres, $genre);
		}
	
		$taglines = $movie->taglines ();
		$i=0;
		foreach($taglines as $line){
			$i++;
			mysql_query("INSERT INTO m_tagline SET movID=".$movSQLid.", tagline=\"".mysql_real_escape_string($line)."\", ordernr=\"$i\"");
		}
	
	
		// CAST MEMBERS
	
		$cast = $movie->director ();
		$i=0;
		foreach($cast as $member){
			$i++;
			mysql_query("INSERT INTO m_cast SET movID=".$movSQLid.", cType=\"director\", cOrdernr=\"$i\", cSubtype=\"".mysql_real_escape_string($member["role"])."\", cName=\"".mysql_real_escape_string($member["name"])."\", cIMDB=\"".$member["imdb"]."\"");
		}
	
		$cast = $movie->writing();
		$i=0;
		foreach($cast as $member){
			$i++;
			mysql_query("INSERT INTO m_cast SET movID=".$movSQLid.", cType=\"author\", cOrdernr=\"$i\", cSubtype=\"".mysql_real_escape_string($member["role"])."\", cName=\"".mysql_real_escape_string($member["name"])."\", cIMDB=\"".$member["imdb"]."\"");
		}
	
		$cast = $movie->producer();
		$i=0;
		foreach($cast as $member){
			$i++;
			mysql_query("INSERT INTO m_cast SET movID=".$movSQLid.", cType=\"producer\", cOrdernr=\"$i\", cSubtype=\"".mysql_real_escape_string($member["role"])."\", cName=\"".mysql_real_escape_string($member["name"])."\", cIMDB=\"".$member["imdb"]."\"");
		}
	
		$cast = $movie->cast();
		$i=0;
		foreach($cast as $member){
			$i++;
			mysql_query("INSERT INTO m_cast SET movID=".$movSQLid.", cType=\"actor\", cOrdernr=\"$i\", cSubtype=\"".mysql_real_escape_string($member["role"])."\", cName=\"".mysql_real_escape_string($member["name"])."\", cIMDB=\"".$member["imdb"]."\"");
		}
	
	
		$plot = $movie->plot ();
		foreach($plot as $txt){
			mysql_query("INSERT INTO m_plot SET movID=".$movSQLid.", plotTXT=\"".mysql_real_escape_string($txt)."\"");
		}
	
		$recommendations = $movie->recommendations ();
		foreach($recommendations as $txt){
			mysql_query("INSERT INTO m_recommend SET movID=".$movSQLid.", recTitle=\"".mysql_real_escape_string($txt)."\"");
		}
	
		foreach($movie->mpaa() as $country=>$certif){
			mysql_query("INSERT INTO m_certificate SET movID=".$movSQLid.", country=\"".mysql_real_escape_string($country)."\", certificate=\"".mysql_real_escape_string($certif)."\"");
		}

		if (($photo_url = $movie->photo_localurl() ) != FALSE){
			echo '<img src="'.$photo_url.'">';
		}

	}else{
		echo "INSERT INTO m_movies SET imdbID=\"".$ID."\", title=\"".mysql_real_escape_string($movie->title())."\", imdbrating=\"".$movie->rating()."\", imdbraters=".$movie->votes().", runtime=".$movie->runtime().", color=\"".mysql_real_escape_string(implode(",",$cols))."\", year=\"".$movie->year()."\", language=\"".$movie->language()."\", aka=\"".mysql_real_escape_string(implode("\n",$akas))."\", country=\"".mysql_real_escape_string(implode(",",$country))."\", sound=\"".mysql_real_escape_string(implode(",",$sound))."\"";
		echo "<br />Error: ".$ID."<br />".mysql_error()."<br /><br />";
		exit;
	}
}

/*
     echo '<TR><TD rowspan=110 valign=top>';
     if (($photo_url = $movie->photo_localurl() ) != FALSE){
	  echo '<img src="'.$photo_url.'">';
     }else{
          echo "No photo available";
     }

     echo '<TR><TD valign=top>';
     echo '<B>Runtime line:</b>';
     echo '</TD><TD>';
     echo $movie->runtime_all ();
     echo '</TD></TR>';

     echo '<TR><TD valign=top>';
     echo '<B>All Runtimes:</b>';
     echo '</TD><TD>';
     $runtimes = $movie->runtimes ();

     foreach ($movie->runtimes() as $runtimes){
	  echo $runtimes["time"]." min in ".$runtimes["country"]." (".$runtimes["comment"].")<BR>";
//	  if ( ($i+1) != count($runtimes)) echo ", ";
     }

     echo '</TD></TR>';
*/
?>
</body>
</html>